package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.floatwindow.FloatWindowService;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/18.
 */

public class FloatSwitchActivity extends BaseActivity {

    @BindView(R.id.switch_bt)
    Switch switchBt;

    public static Intent floatService = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_float_swith);
        ButterKnife.bind(this);
        if(SPUtil.getInt(FloatSwitchActivity.this, "service") == 1){
            switchBt.setChecked(true);
        }else{
            switchBt.setChecked(false);
        }
        switchBt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    if(floatService == null){
                        SPUtil.put(FloatSwitchActivity.this, "service", 1);
                        floatService = new Intent(FloatSwitchActivity.this, FloatWindowService.class);
                        startService(floatService);
                    }
                }else{
                    if(floatService != null){
                        SPUtil.put(FloatSwitchActivity.this, "service", 0);
//                        Intent intent = new Intent(FloatSwitchActivity.this, FloatWindowService.class);
                        stopService(floatService);
                        floatService = null;
                    }
                }
                finish();
            }
        });
    }
}
